/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core.io;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 *
 * @author Dung Pham
 */
public final class MessageResponseEncoder extends ProtocolEncoderAdapter {

    @Override
    public void encode(IoSession session, Object obj, ProtocolEncoderOutput output) throws Exception {
        if (!(obj instanceof Message)) {
            String error = "Endcode : data is not expected. Wrong Protocol format";
            session.close(true);
            throw new Exception(error);
        }

        try {
            Message message = (Message) obj;
            //// Use for router client protocol.
            /*if (session.containsAttribute(AttributeName.ROUTING_ATTRIBUTE)) {
             IoBuffer buffer = IoBuffer.wrap(message.toByteArrayEx());
             outPut.write(buffer);
             return;
             }*/

            //// Apply for new protocol
            if (session.containsAttribute(Attribute.SESSION_INFOR)) {
                SessionAttribute sessionInfo = (SessionAttribute) session.getAttribute(Attribute.SESSION_INFOR);
                ExchangeKeyAttribute cryptoInfor = (ExchangeKeyAttribute) session.getAttribute(Attribute.EXCHANGE_KEY_ATTRIBUTE);
                //// Encrypt message.
                //Reporter.debug(">>>>> Send response with new protocol");
                IoBuffer buffer;
                if (session.containsAttribute(Attribute.PROTOCOL_10_ATTRIBUTE)) {
                    buffer = IoBuffer.wrap(message.toByteArrayProtocol10(cryptoInfor.getTea()));
                } else {
                    buffer = IoBuffer.wrap(message.toByteArrayEx(cryptoInfor.getTea()));
                }
                //session.setAttribute(AttributeName.LAST_ACTIVE_TIME, System.currentTimeMillis());
                output.write(buffer);
                return;

            }
            ////////////////////////////////////////////////////////////////////
            if (session.containsAttribute(Attribute.EXCHANGE_KEY_ATTRIBUTE)) {
                ExchangeKeyAttribute cryptoInfor = (ExchangeKeyAttribute) session.getAttribute(Attribute.EXCHANGE_KEY_ATTRIBUTE);
                //// Encrypt message.
                IoBuffer buffer = IoBuffer.wrap(message.toByteArray(cryptoInfor.getTea()));
                //session.setAttribute(AttributeName.LAST_ACTIVE_TIME, System.currentTimeMillis());
                output.write(buffer);
            } else {
                //Reporter.warn("Send an unsecure message.");
                IoBuffer buffer = IoBuffer.wrap(message.toByteArray());
                //session.setAttribute(AttributeName.LAST_ACTIVE_TIME, System.currentTimeMillis());
                output.write(buffer);
            }

        } catch (Exception ex) {
            //Reporter.error("endcode got exception :", ex);
        }
    }
}
